package test1.service;

import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import test1.entity.Account;
import test1.mapper.AccountMapper;

/**
 * @author: mornd
 * @dateTime: 2023/5/15 - 20:33
 */
@Service
public class AccountService2 {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private AccountService2 accountService;

    @Transactional(rollbackFor = Exception.class)
    public void transfer(int from, int to, double money) throws Throwable {
        //  直接拿到当前类的代理对象，前提是配置：@EnableAspectJAutoProxy(exposeProxy = true)
        Object proxy = AopContext.currentProxy();
        accountService.foo();
        Account account = accountMapper.getById(from);
        if(account != null && (account.getBalance() - money >= 0)) {
            accountMapper.update(from, -money);
            accountMapper.update(to, money);
        }
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void foo() {
        System.out.println(this.getClass());
    }
}
